<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="应用ID" prop="applicationId">
        <el-select v-model="queryParams.applicationId" placeholder="请选择应用ID" clearable size="small">
          <el-option
            v-for="dict in applicationIdOptions"
            :key="dict.appCode"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推送类型" prop="pushType">
        <el-select v-model="queryParams.pushType" placeholder="请选择推送类型" clearable size="small">
          <el-option
            v-for="dict in pushTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商家类型" prop="merchantType">
        <el-select v-model="queryParams.merchantType" placeholder="请选择商家类型" clearable size="small">
          <el-option
            v-for="dict in merchantTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否可用" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择是否可用" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['baseconfig:pushconfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['baseconfig:pushconfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['baseconfig:pushconfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['baseconfig:pushconfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pushconfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="应用ID" align="center" prop="applicationId" :formatter="applicationIdFormat" />
      <el-table-column label="应用唯一编码" align="center" prop="appKey" />
      <el-table-column label="应用秘钥" align="center" prop="appSecret" />
      <el-table-column label="签名方式" align="center" prop="signType" :formatter="signTypeFormat" />
      <el-table-column label="签名" align="center" prop="smsSign" />
      <el-table-column label="推送类型" align="center" prop="pushType" :formatter="pushTypeFormat" />
      <el-table-column label="商家类型" align="center" prop="merchantType" :formatter="merchantTypeFormat" />
      <el-table-column label="是否可用" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['baseconfig:pushconfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseconfig:pushconfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改推送参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="应用ID" prop="applicationId">
          <el-select v-model="form.applicationId" placeholder="请选择应用ID">
            <el-option
              v-for="dict in applicationIdOptions"
              :key="dict.appCode"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应用唯一编码" prop="appKey">
          <el-input v-model="form.appKey" placeholder="请输入应用唯一编码" />
        </el-form-item>
        <el-form-item label="应用秘钥" prop="appSecret">
          <el-input v-model="form.appSecret" placeholder="请输入应用秘钥" />
        </el-form-item>
        <el-form-item label="签名方式" prop="signType">
          <el-select v-model="form.signType" placeholder="请选择签名方式">
            <el-option
              v-for="dict in signTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="签名" prop="smsSign">
          <el-input v-model="form.smsSign" placeholder="请输入签名" />
        </el-form-item>
        <el-form-item label="推送类型" prop="pushType">
          <el-select v-model="form.pushType" placeholder="请选择推送类型">
            <el-option
              v-for="dict in pushTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商家类型" prop="merchantType">
          <el-select v-model="form.merchantType" placeholder="请选择商家类型">
            <el-option
              v-for="dict in merchantTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可用Y是N否">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预留参数1" prop="parameter1">
          <el-input v-model="form.parameter1" placeholder="请输入预留参数1" />
        </el-form-item>
        <el-form-item label="预留参数1" prop="parameter2">
          <el-input v-model="form.parameter2" placeholder="请输入预留参数1" />
        </el-form-item>
        <el-form-item label="预留参数1" prop="parameter3">
          <el-input v-model="form.parameter3" placeholder="请输入预留参数1" />
        </el-form-item>
        <el-form-item label="预留参数1" prop="parameter4">
          <el-input v-model="form.parameter4" placeholder="请输入预留参数1" />
        </el-form-item>
        <el-form-item label="预留参数1" prop="parameter5">
          <el-input v-model="form.parameter5" placeholder="请输入预留参数1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPushconfig, getPushconfig, delPushconfig, addPushconfig, updatePushconfig, exportPushconfig } from "@/api/baseconfig/pushconfig";
import {listApplication} from "@/api/config/application";

export default {
  name: "Pushconfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推送参数配置表格数据
      pushconfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 应用ID字典
      applicationIdOptions: [],
      // 签名方式字典
      signTypeOptions: [],
      // 推送类型sms  app webhook window字典
      pushTypeOptions: [],
      // 商家类型字典
      merchantTypeOptions: [],
      // 是否可用Y是N否字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicationId: null,
        pushType: null,
        merchantType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getApplicationList();
    this.getList();
    this.getDicts("sys_push_sign_type").then(response => {
      this.signTypeOptions = response.data;
    });
    this.getDicts("sys_channel_push_type").then(response => {
      this.pushTypeOptions = response.data;
    });
    this.getDicts("sys_channel_merchant_type").then(response => {
      this.merchantTypeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询推送参数配置列表 */
    getList() {
      this.loading = true;
      listPushconfig(this.queryParams).then(response => {
        this.pushconfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getApplicationList() {
      listApplication({status: 'Y'}).then(response => {
        this.applicationIdOptions = response.rows;
      });
    },
    // 应用ID字典翻译
    applicationIdFormat(row, column) {
      return this.selectDictLabel(this.applicationIdOptions, row.applicationId);
    },
    // 签名方式字典翻译
    signTypeFormat(row, column) {
      return this.selectDictLabel(this.signTypeOptions, row.signType);
    },
    // 推送类型sms  app webhook window字典翻译
    pushTypeFormat(row, column) {
      return this.selectDictLabel(this.pushTypeOptions, row.pushType);
    },
    // 商家类型字典翻译
    merchantTypeFormat(row, column) {
      return this.selectDictLabel(this.merchantTypeOptions, row.merchantType);
    },
    // 是否可用Y是N否字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applicationId: null,
        appKey: null,
        appSecret: null,
        signType: null,
        smsSign: null,
        pushType: null,
        merchantType: null,
        status: "0",
        remark: null,
        parameter1: null,
        parameter2: null,
        parameter3: null,
        parameter4: null,
        parameter5: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加推送参数配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPushconfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改推送参数配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePushconfig(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPushconfig(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除推送参数配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPushconfig(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有推送参数配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPushconfig(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
